// 定义参数
const params = {
    pagenum: 1,
    pagesize: 5,
    cate_id: '',
    state: ''
}

// 获取数据方法
function getList() {
    $.ajax({
        url: '/my/article/list',
        data: params,
        success: function (res) {
            const temp = template('list', res)
            $('tbody').html(temp)
            renderPage(res.total)
        }
    })
}
getList()

$('body').on('click', '.remove', function () {
    $.ajax({
        url: '/my/article/info?id=' + $(this).attr('data-id'),
        method: 'DELETE',
        success: function (res) {
            if (res.code === 0) {
                getList()
            }
            layui.layer.msg(res.message)
        }
    })
})

function addZero(num) {
    return num < 10 ? '0' + num : num
}
template.defaults.imports.dateFormat = function (value) {
    const date = new Date(value),
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds()
    return `${(year)}-${addZero(month)}-${addZero(month)}-${addZero(day)}-${addZero(hour)}-${addZero(min)}-${addZero(sec)}`
}

// 获取分类数据
$.ajax({
    url: '/my/cate/list',
    success: function (res) {
        const temp = template('cate-list', res)
        $('.cate-list').html(temp)
        layui.form.render()
    }
})

// 筛选
$('.search-form').submit(function (e) {
    e.preventDefault()
    params.cate_id = $('.cate-list').val()
    params.state = $('.state-list').val()
    getList()
})

//分页
function renderPage(total) {
    layui.laypage.render({
        elem: 'page-box',
        count: total,
        limit: params.pagesize,
        limits: [5, 10, 20],
        curr: params.pagenum,//设置默认被选中的分页
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function (obj, first) {
            if (first) return
            params.pagenum = obj.curr
            params.pagesize = obj.limit
            getList()
        }
    })
}